package com.happystreet.config
{
	import com.happystreet.info.GoodInfo;
	import com.happystreet.manager.CrowdManager;
	/**
	 * 货物配置
	 * @author Liseen
	 * 
	 */	
	public class GoodsConfig
	{
		public static var goods:Object;
		public static var shopGoods:Object;
		
		public function GoodsConfig()
		{
		}
		
		public static function getGoodsListByShopType(type:int):Array
		{
			return shopGoods[type];
		}
		
		public static function getGoodDeliveryTimeById(id:int):int
		{
			var goodInfo:GoodInfo = goods[id];
			return goodInfo.timePerPack;
		}
		
		public static function getGoodCostPerPackById(id:int):int
		{
			var info:GoodInfo = goods[id];
			return info.costPerPack;
		}

		/**
		 *通过元件名获取货物id 
		 * @param name
		 * @return 
		 * 
		 */		
		public static function getGoodIdBySymbolName(name:String):String
		{
			var goodId:String;
			var info:GoodInfo;
			for (goodId in goods)
			{
				info = goods[goodId];
				if (info.symbolName == name)
				{
					return goodId;
				}
			}
			return null;
		}
		/**
		 *根据id获取货物的默认价格 
		 * @param id
		 * @return 
		 * 
		 */		
		public static function getGoodDefaultPriceById(id:int):int
		{
			var info:GoodInfo = goods[id];
			return info.defaultPrice;
		}
		
		/**
		 *通过id获取目标商店类型 
		 * @param id
		 * @return 
		 * 
		 */		
		public static function getTargetShopTypeByGoodId(id:int):int
		{
			var info:GoodInfo = goods[id];
			if (info != null)
			{
				return info.shopType;
			}
			return 0;
		}
		/**
		 *设置货物配置 
		 * @param config
		 * 
		 */		
		public static function setGoodsConfig(config:String):void
		{
			var good:XML;
			var infoList:Array;
			var goodInfo:GoodInfo;
			var configXML:XML = new XML(config);
			goods = new Object();
			shopGoods = new Object();
			var goodXMLList:XMLList = configXML..good;
			for each(good in goodXMLList)
			{
				goodInfo = new GoodInfo(good);
				goods[good.@id] = goodInfo;
				
				if(goodInfo.shopType==int(good.@shopType)){
					if (!shopGoods.hasOwnProperty(good.@shopType))
					{
						shopGoods[good.@shopType] = [];
					}
					shopGoods[good.@shopType].push(goodInfo);
				}
			}
			
			for each (infoList in shopGoods)
			{
				infoList.sortOn("goodId", Array.NUMERIC);
			}

		}
		
		/**
		 *根据id获取材料库存 
		 * @param id
		 * @return 
		 * 
		 */		
		public static function getMaterialStockById(id:int):int
		{
			var stock:int;
			if (GameConfig.userInfo.hasOwnProperty("materials"))
			{
				stock = GameConfig.userInfo.materials[String(id)];
			}
			return stock;
		}
		
		/**
		 *依据商店数据设置货物等级 
		 * @param shopData
		 * 
		 */		
		public static function setGoodsLevelsFromShopData(shopData:Array):void
		{
			var data:Object;
			var level:String;
			var info:GoodInfo;
			for each (data in shopData)
			{
				if (data.hasOwnProperty("goodsLevel"))
				{
					for (level in data.goodsLevel)
					{
						info = (GoodsConfig.goods[level] as GoodInfo);
						info.level = data.goodsLevel[level];
					}
				}
			}
		}
		
		public static function checkIsGoodPurchased(_arg1:int):Boolean
		{
			var spawner:Object;
			var _local3:int;
			for each (spawner in CrowdManager.instance.spawnersConfigList)
			{
				for each (_local3 in spawner.goods)
				{
					if (_local3 == _arg1)
					{
						return true;
					}
				}
			}
			return false;
		}
		/**
		 *根据商店类型获取货物id列表 
		 * @param type
		 * @return 
		 * 
		 */		
		public static function getGoodsIdsByShopType(type:int):Array
		{
			var info:GoodInfo;
			var idList:Array = new Array();
			for each (info in shopGoods[type])
			{
				idList.push(info.goodId);
			}
			return idList;
		}
	}
}